package com.wdhcr.service.imp;

import com.wdhcr.entity.Blog;
import com.wdhcr.entity.Tag;
import com.wdhcr.entity.Type;
import com.wdhcr.entity.weixin.Article;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.repository.BlogRepository;
import com.wdhcr.service.BlogService;
import com.wdhcr.service.UserService;
import com.wdhcr.utils.ResponseResult;
import com.wdhcr.utils.WxUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/*
 * @description: blog逻辑的service实现类
 * @author: wdhcr
 * @dateTime: 2020/3/19 20:18
 */
@Service
public class BlogServiceImp implements BlogService {

    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private UserService userService;

    @Autowired
    private WxUtil wxUtil;

    @Value("${demoUrl}")
    private String demoUrl;



    @Override
    public Blog saveBlog(Blog blog) {
        if (blog.getId() == null) {
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
            Blog blog1 = blogRepository.save(blog);
            if (blog.getPublished()){
                //当新添加一篇博客时，向每一个用户推送一个图文消息
                Article article = new Article(blog1.getTitle(), blog1.getDescription(), blog1.getFirstImageUrl(), demoUrl+"/#/sBlog?id="+ blog1.getId());
                Set<String> openidAll = userService.findOpenidAll();
                for (String s : openidAll) {
                    wxUtil.sendNewsMessage(s,article);
                }
            }
            return blog1;
        } else {
            int i = blogRepository.updateBlog(blog.getId(), blog.getTitle(), blog.getContent(),
                    blog.getHtml(), blog.getFirstImageUrl(), blog.getFlag(), blog.getPublished(),
                    new Date(), blog.getDescription(), blog.getTag(), blog.getType());
            if (i != 0) {
                return new Blog();
            }
        }
        return null;
    }

    @Override
    public List<Blog> findAll() {
        //查询博客列表
        return blogRepository.findAll();
    }

    /*
     * @description: 分页查询所有
     * @params: [pageable]
     * @return: org.springframework.data.domain.Page<com.wdhcr.entity.Blog>
     * @author: wdhcr
     * @dateTime: 2020/3/21 21:16
     */
    @Override
    public Page<Blog> findAll(Pageable pageable) {
        return blogRepository.findAll(pageable);
    }

    //分页查询所有公开的博客
    @Override
    public Page<Blog> findPubAll(Pageable pageable) {
        return blogRepository.findPubAll(pageable);
    }

    @Override
    public List<Blog> findPubTop(Integer size) {
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"updateTime");
        PageRequest pageRequest = PageRequest.of(0, size,update_time);
        return findPubAll(pageRequest).toList();
    }

    /*
     * @description:根据前端id查询博客
     * @params: [id]
     * @return: com.wdhcr.entity.Blog
     * @author: wdhcr
     * @dateTime: 2020/3/21 22:22
     */
    @Override
    public Blog findById(Integer id) {
        Blog blog = blogRepository.findById(id.longValue()).get();
        blog.setViews(blog.getViews()+1);
        saveBlog(blog);
        return blog;
    }

    @Override
    public Page<Blog> findByQuery(Pageable pageable, String query) {
        return blogRepository.findByQuery("%" + query + "%", pageable);
    }

    @Override
    public List<Blog> findByQueryPub(Sort sort, String query) {
        return blogRepository.findByQueryPub("%" + query + "%",sort);
    }

    @Override

    public Page<Blog> findBlogByType(Pageable pageable, Type type) {
        if (type.getId() !=null&&type.getName() != null){
            return blogRepository.findBlogByTypeAndPublishedTrue(type,pageable);
        }else {
            return blogRepository.findPubAll(pageable);
        }
    }

    @Override

    public Page<Blog> findBlogByTag(Pageable pageable, Tag tag) {
        if (tag.getId() !=null&&tag.getName() != null){
            return blogRepository.findBlogByTagAndPublishedTrue(tag,pageable);
        }else {
            return blogRepository.findPubAll(pageable);
        }
    }

    @Override

    public ResponseResult deleteById(Integer id) {
        Blog one = blogRepository.getOne(id.longValue());
        if (one !=null){
            if (one.getPublished()){
                one.setPublished(false);
                Blog blog = new Blog();
                BeanUtils.copyProperties(one,blog);
                Blog blog1 = saveBlog(blog);
                if (blog1 != null){
                    return new ResponseResult(ResultCode.SUCCESS.getCode(),"已放入回收站!",null);
                }
            }else {
                blogRepository.deleteById(id.longValue());
                return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除成功!",null);
            }
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除失败!",null);
    }
}
